<?php //if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	define ('BASEPATH' , substr(__FILE__,0,strlen(__FILE__)-strlen('system/load.inc.php')));

	if ( ! function_exists('is_php'))
	{
		function is_php($version = '5.0.0')
		{
			static $_is_php;
			$version = (string)$version;

			if ( ! isset($_is_php[$version]))
			{
				$_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
			}

			return $_is_php[$version];
		}
	}
	/*
	* Load một file và tạo một đối tượng
	*/
	if(! function_exists ('load_class'))
	{
		function &load_class($class, $directory, $prefix='.cls')
		{
			static $_classes = array();
			// If class exists, return this class in static
			if (isset($_classes[$class]))
			{
				return $_classes[$class];
			}
			$name = FALSE;
			
			if (file_exists(BASEPATH.$directory.'/'.$class.'.php'))
			{
				$name = substr($class,0,strlen($class)-strlen($prefix));
				if(class_exists($name)=== FALSE)
				{
					require(BASEPATH.$directory.'/'.$class.'.php');
				}
			}
			/*if(file_exists($BASEPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
				echo "Ok";*/
			if ($name === FALSE)
			{
				exit('Unable to locate the specified class: '.$class.'.php');
			}
			//is_loaded($class); //Luu nhung class da load roi, de kiem tra lan sau
			$_classes[$class] = new $name();
			return $_classes[$class];
		}
	}
	/*
	*	get all config from ./config/config.php
	*/
	if(!function_exists('get_config'))
	{
		function &get_config($replace = array())
		{
			static $_config;
			if(isset($_config))
			{
				return $_config[0];
			}
			$filePath = BASEPATH.'config/config.php';
			if(!file_exists($filePath))
			{
				exit('File config.php doesnt exsits!');
			}
			require($filePath);
			if(!isset($config) OR !is_array($config))
			{
				exit('Cant not read file as array or no data in this');
			}
			if(count($replace)>0)
			{
				foreach($replace as $key => $value)
				{
					if (isset($config[$key]))
					{
						$config[$key] = $value;
					}
				}
			}
			return $_config[0] =& $config;
		}
	}
	if(!function_exists('get_config_item'))
	{
		function get_config_item($key)
		{
			static $_config_item = array();
			if(!isset($_config_item[$key]))
			{
				$config = &get_config();
				if( !isset($config[$key]) )
					return FALSE;
				$_config_item[$key] = $config[$key];
			}
			return $_config_item[$key];
		}
	}
	/*
	* Hiển thị lỗi 
	*/
	if ( ! function_exists('show_error'))
	{
		function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
		{
			$_error =& load_class('Exceptions', 'system');
			echo $_error->show_error($heading, $message, 'error_general', $status_code);
			exit;
		}
	}
	/*
	* Xử lý header
	*/
	if ( ! function_exists('set_status_header'))
	{
		function set_status_header($code = 200, $text = '')
		{
			$stati = array(
								200	=> 'OK',
								201	=> 'Created',
								202	=> 'Accepted',
								203	=> 'Non-Authoritative Information',
								204	=> 'No Content',
								205	=> 'Reset Content',
								206	=> 'Partial Content',

								300	=> 'Multiple Choices',
								301	=> 'Moved Permanently',
								302	=> 'Found',
								304	=> 'Not Modified',
								305	=> 'Use Proxy',
								307	=> 'Temporary Redirect',

								400	=> 'Bad Request',
								401	=> 'Unauthorized',
								403	=> 'Forbidden',
								404	=> 'Not Found',
								405	=> 'Method Not Allowed',
								406	=> 'Not Acceptable',
								407	=> 'Proxy Authentication Required',
								408	=> 'Request Timeout',
								409	=> 'Conflict',
								410	=> 'Gone',
								411	=> 'Length Required',
								412	=> 'Precondition Failed',
								413	=> 'Request Entity Too Large',
								414	=> 'Request-URI Too Long',
								415	=> 'Unsupported Media Type',
								416	=> 'Requested Range Not Satisfiable',
								417	=> 'Expectation Failed',

								500	=> 'Internal Server Error',
								501	=> 'Not Implemented',
								502	=> 'Bad Gateway',
								503	=> 'Service Unavailable',
								504	=> 'Gateway Timeout',
								505	=> 'HTTP Version Not Supported'
							);

			if ($code == '' OR ! is_numeric($code))
			{
				show_error('Status codes must be numeric', 500);
			}

			if (isset($stati[$code]) AND $text == '')
			{
				$text = $stati[$code];
			}

			if ($text == '')
			{
				show_error('No status text available.  Please check your status code number or supply your own message text.', 500);
			}

			$server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;

			if (substr(php_sapi_name(), 0, 3) == 'cgi')
			{
				header("Status: {$code} {$text}", TRUE);
			}
			elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
			{
				header($server_protocol." {$code} {$text}", TRUE, $code);
			}
			else
			{
				header("HTTP/1.1 {$code} {$text}", TRUE, $code);
			}
		}
	}
	/*
	*	Bỏ các ký tự ẩn
	*/
	if ( ! function_exists('remove_invisible_characters'))
	{
		function remove_invisible_characters($str, $url_encoded = TRUE)
		{
			$non_displayables = array();
			
			// every control character except newline (dec 10)
			// carriage return (dec 13), and horizontal tab (dec 09)
			
			if ($url_encoded)
			{
				$non_displayables[] = '/%0[0-8bcef]/';	// url encoded 00-08, 11, 12, 14, 15
				$non_displayables[] = '/%1[0-9a-f]/';	// url encoded 16-31
			}
			
			$non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';	// 00-08, 11, 12, 14-31, 127

			do
			{
				$str = preg_replace($non_displayables, '', $str, -1, $count);
			}
			while ($count);

			return $str;
		}
	}
	/*
	*	Trả về kiểu charset
	*/
	if ( ! function_exists('html_escape'))
	{
		function html_escape($var)
		{
			if (is_array($var))
			{
				return array_map('html_escape', $var);
			}
			else
			{
				return htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
			}
		}
	}
	/*
	*	Test load.inc.php
	*//*class abc
	{
		private $config = array();
		public function display()
		{
			$config= &get_config();
			$key = 'Restaurant';
			$config[$key] = &load_class('Format','lib');
			var_dump($config);
		}
	}
	$test = new abc();
	$test->display();*/
?>